<?php


  class CeshiAction extends Action{
  
  
     public function fanyong(){
	 
	   
	   $order=M('buyer')->where('orderno="%s"',$orderno)->find();
	   
	   //这里用户
	   $user=M('dailiuser')->where('id="%s"',$order['uid'])->find();
	   
	   
	   $yongjin=M('yongjin')->where('id=1')->find();  
	   
	   //如果有上级，第一层返利开始
	   if($user['upid']){
	   
	   $up1=M('dailiuser')->where('id="%s"',$user['upid'])->find();
	   
	   $yiji=$order['money']*($yongjin['yongjin1']/100);

	   $yiji  = round($yiji,2);
	   //加入用户余额
	   $data['money'] = $user['money']+$yiji;
	   M('dailiuser')->where('id='.$user['id'])->save($data);
	   //返佣记录写入数据库
	   $fan['fid'] = $order['uid'];
	   $fan['bid'] = $user['upid'];
	   $fan['money'] = $yiji;
	   $fan['time'] = time();
	   $fan['vid'] = $order['vid'];
	   $fan['ceng'] = 1;
	   M('fanli')->add($fan);

	   }
	   //如果一层有上级，第二层返利开始
	   if($up1['upid']){
	   
	   $up2=M('dailiuser')->where('id="%s"',$up1['upid'])->find();
	   
	   $erji=$order['money']*($yongjin['yongjin2']/100);

	   $erji  = round($erji,2);
	   //加入用户余额
	   $data1['money'] = $up2['money']+$erji;
	   M('dailiuser')->where('id='.$up2['id'])->save($data1);
	   //返佣记录写入数据库
	   $fan1['fid'] = $order['uid'];
	   $fan1['bid'] = $up1['upid'];
	   $fan1['money'] = $erji;
	   $fan1['time'] = time();
	   $fan1['vid'] = $order['vid'];
	   $fan1['ceng'] = 2;
	   M('fanli')->add($fan1);

	   }
	   //如果有二层有上级，第三层返利开始
	   if($up2['upid']){
	   
	   $up3=M('dailiuser')->where('id="%s"',$up2['upid'])->find();
	   
	   $sanji=$order['money']*($yongjin['yongjin3']/100);

	   $sanji  = round($sanji,2);
	   //加入用户余额
	   $data2['money'] = $up3['money']+$sanji;
	   M('dailiuser')->where('id='.$up3['id'])->save($data2);
	   //返佣记录写入数据库
	   $fan2['fid'] = $order['uid'];
	   $fan2['bid'] = $up2['upid'];
	   $fan2['money'] = $sanji;
	   $fan2['time'] = time();
	   $fan2['vid'] = $order['vid'];
	   $fan2['ceng'] = 3;
	   M('fanli')->add($fan2);

	   }
	    
	 
	   
	 
	 
	 
	 }
  
  
  
     public function index(){
	 
	 
	          $wx=M('wx')->where('id=1')->find();
                 $APPID=$wx['appid'];
                 $APPSECRET=$wx['appsecret'];
                 $mytoken=M('token')->where('id=1')->find();


                $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;



				if(time()>$mytoken['time']){
				
				$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
				
				$json=file_get_contents($TOKEN_URL);
				$result=json_decode($json);
				
				
				$ACC_TOKEN=$result->access_token;
				
				$st['token']=$ACC_TOKEN;
				$st['time']=time()+3600;
				M('token')->where('id=1')->save($st);
				
				}else{
				$ACC_TOKEN=$mytoken['token'];
				}
				
				
				
				$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$ACC_TOKEN."&openid=oXn7-wx5rqVP7r-M_HVgYRumJDws&lang=zh_CN";
				
				$re=file_get_contents($url);
	 
	            dump($re);
	 
	 }
  
  
  }